<?php

include_once APPLICATION_DIR . "/application/model/PRESS.php";

class Press extends DataObjects_PRESS
{
	public function validateInput(){
        global $error_msg;

		$posted_file = $_FILES["IMAGE"];
		if ($error_msg->isNoError() && !empty($posted_file["name"])) {
			if ($this->ID) {
				$this->OLD_IMAGE = $this->IMAGE;
			}
            $upload = new UPLOAD();
			$new_file = $upload->doUploadImage(IMAGE_DIR."/", 'IMAGE', '', '190', '');
			if ($new_file) {
				$this->IMAGE = $new_file;
			} else {
				$error_msg->add('_MSG_UPLOAD_FAILED');
			}
		}

		return $error_msg->isNoError();
    }

	public function update() {
		if (!empty($this->OLD_IMAGE) && $this->IMAGE != $this->OLD_IMAGE) {
			@unlink(IMAGE_DIR."/{$this->OLD_IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->OLD_IMAGE}");
		}

		return parent::update();
	}

	public function delete() {
		if ($this->IMAGE) {
			@unlink(IMAGE_DIR."/{$this->IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->IMAGE}");
		}

		return parent::delete();
	}

	public static function getDir()
	{
		return IMAGE_DIR;
	}

	public function getPath()
	{
		return self::getDir()."/{$this->IMAGE}";
	}
}
?>
